package com.zlx.store.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.zlx.store.common.constant.SecurityConstant;
import com.zlx.store.domain.entity.StoreRelateUser;
import com.zlx.store.domain.entity.User;
import com.zlx.store.domain.vo.UserVo;
import com.zlx.store.user.dao.StoreRelateUserDao;
import com.zlx.store.user.dao.UserDao;
import com.zlx.store.user.service.UserService;
import org.apache.lucene.search.similarities.Lambda;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;
    @Autowired
    StoreRelateUserDao storeRelateUserDao;

    @Override
    public int addUser(UserVo user) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUserName, user.getUser().getUserName());
        if (Objects.nonNull(userDao.selectOne(wrapper))){
            return 0;
        }
        int result=userDao.insert(user.getUser());
        if (user.getUser().getUserRole().equals(SecurityConstant.USER_ROLE_MANAGER.getCode())) {
            Long userId= userDao.selectOne(wrapper).getUserId();
            List<Long> storeIds=user.getStoreIds();
            for (Long storeId:storeIds){
                StoreRelateUser storeRelateUser=new StoreRelateUser();
                storeRelateUser.setStoreId(storeId);
                storeRelateUser.setUserId(userId);
                result += storeRelateUserDao.insert(storeRelateUser);
            }
        }
        return result;
    }

    @Override
    public int editUser(@RequestBody UserVo user) {
        int result=userDao.updateById(user.getUser());
        if (user.getUser().getUserRole().equals(SecurityConstant.USER_ROLE_MANAGER.getCode())) {
            Long userId=user.getUser().getUserId();
            List<Long> storeIds=user.getStoreIds();
            LambdaQueryWrapper<StoreRelateUser> wrapper=new LambdaQueryWrapper<>();
            wrapper.eq(StoreRelateUser::getUserId,userId);
            List<StoreRelateUser> storeRelateUsers=storeRelateUserDao.selectList(wrapper);
            for (StoreRelateUser storeRelateUser:storeRelateUsers){
                if (storeIds.contains(storeRelateUser.getStoreId())){
                    storeIds.remove(storeRelateUser.getStoreId());
                }
                else {
                    storeRelateUserDao.deleteById(storeRelateUser.getId());
                }
            }
            for (Long storeId:storeIds){
                StoreRelateUser storeRelateUser=new StoreRelateUser();
                storeRelateUser.setStoreId(storeId);
                storeRelateUser.setUserId(userId);
                storeRelateUserDao.insert(storeRelateUser);
            }
        }
        return result;
    }

    @Override
    public int delUser(Long id) {
      LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserId,id);
        Integer role=userDao.selectOne(queryWrapper).getUserRole();
        UpdateWrapper<User> wrapper=new UpdateWrapper<>();
        wrapper.eq("user_id",id);
        wrapper.set("user_role",SecurityConstant.USER_ROLE_DELETE.getCode());
        int result=userDao.update(null,wrapper);
        if (role.equals(SecurityConstant.USER_ROLE_MANAGER.getCode())){
            LambdaQueryWrapper<StoreRelateUser> wrapper1=new LambdaQueryWrapper<>();
            wrapper1.eq(StoreRelateUser::getUserId,id);
            storeRelateUserDao.delete(wrapper1);
        }
        return result;
    }

    @Override
    public IPage<User> showAllUser(int currentPage,int size,Integer role) {
        IPage<User> page=new Page<>();
        page.setCurrent(currentPage);
        page.setSize(size);
        LambdaQueryWrapper<User> wrapper=new LambdaQueryWrapper<>();
        if (Objects.nonNull(role)){
            wrapper.eq(User::getUserRole,role);
        }
        IPage<User> users=userDao.selectPage(page,wrapper);
        return users;
    }

    @Override
    public UserVo selectUserVoById(Long id) {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("user_id",id);
        User user=userDao.selectOne(wrapper);
        UserVo userVo=new UserVo();
        userVo.setUser(user);
        if (user.getUserRole().equals(SecurityConstant.USER_ROLE_MANAGER.getCode())){
            LambdaQueryWrapper<StoreRelateUser> wrapper1=new LambdaQueryWrapper<>();
            wrapper1.eq(StoreRelateUser::getUserId,id);
            List<StoreRelateUser> storeRelateUsers=storeRelateUserDao.selectList(wrapper1);
            List<Long> storeIds=new ArrayList<>();
            for (StoreRelateUser storeRelateUser:storeRelateUsers){
                storeIds.add(storeRelateUser.getStoreId());
            }
            userVo.setStoreIds(storeIds);
        }
        return userVo;
    }

    @Override
    public User selectUserById(Long id) {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("user_id",id);
        User user=userDao.selectOne(wrapper);
        return user;
    }

    @Override
    public User selectUserByName(String username) {
        LambdaQueryWrapper<User> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(User::getUserName,username);
        wrapper.eq(User::getUserRole, SecurityConstant.USER_ROLE_CUSTOMER.getCode());
        User user=userDao.selectOne(wrapper);
        return user;
    }

    @Override
    public User selectUserByPhone(String phone) {
        LambdaQueryWrapper<User> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(User::getUserPhoneNumber,phone);
        wrapper.eq(User::getUserRole, SecurityConstant.USER_ROLE_CUSTOMER.getCode());
        User user=userDao.selectOne(wrapper);
        return user;
    }

    @Override
    public User selectUserByMail(String mail) {
        LambdaQueryWrapper<User> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(User::getUserEmail,mail);
        wrapper.eq(User::getUserRole, SecurityConstant.USER_ROLE_CUSTOMER.getCode());
        User user=userDao.selectOne(wrapper);
        return user;
    }
}
